package com.sihuatech.coreagent.redis;

import java.util.Collection;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.sihuatech.coreagent.controller.VasController;
import com.sihuatech.coreagent.core.communication.entity.CodeRelationship;
import com.sihuatech.coreagent.core.communication.entity.RequestSetting;
import com.sihuatech.coreagent.mapper.CodeRelationshipMapper;
import com.sihuatech.coreagent.mapper.RequestSettingMapper;
import com.sihuatech.coreagent.until.Constants;


@Component
public class RedisManager {
	private static final Logger logger = LoggerFactory.getLogger(RedisManager.class);
	@Autowired
	RedisTemplate<Object,Object> redisTemplate;
	@Autowired
	RequestSettingMapper requestSettingMapper;
	@Autowired
	CodeRelationshipMapper codeRelationshipMapper;
	public RequestSetting getInfo(String interface_type , String platform_code , String provice_name ){
		
			if(StringUtils.isNotBlank(interface_type) && StringUtils.isNotBlank(provice_name)){
				RequestSetting requestSetting = null;
				CodeRelationship codeRelationship = getCodeRelationship(provice_name,platform_code);
				String province_code = codeRelationship.getProvinceCode();
				try {			
				logger.info(" ++++ redis get key: "+interface_type+province_code+platform_code);
				 requestSetting =(RequestSetting) redisTemplate.opsForValue().get(interface_type+province_code+platform_code);
				} catch (Exception e) {
					logger.error("查询缓存异常"+e);
					logger.info("查询缓存异常,查询数据库获取信息");
				}
				if(requestSetting==null){
					Map<String, Object> columnMap = Maps.newHashMap();
					columnMap.put("interface_type", interface_type);
					columnMap.put("platform_code", platform_code);
					columnMap.put("province_code", province_code);
					List<RequestSetting> list = requestSettingMapper.selectByMap(columnMap);
					addInfo(interface_type, province_code, platform_code, list.get(0));
					requestSetting=(list==null)?null:list.get(0);
				}
				logger.info("根据接口类型interface_type:" + interface_type + ",平台代码platform_code:" + platform_code
						+ ",省份编码province_code："+province_code+",找到RequestSetting信息为："+requestSetting.toString());
				return requestSetting;
				
			}else if(StringUtils.isNotBlank(interface_type)){
				Map<String, Object> columnMap = Maps.newHashMap();
				columnMap.put("interface_type", interface_type);
				columnMap.put("platform_code", platform_code);
				List<RequestSetting> list = requestSettingMapper.selectByMap(columnMap);
				return (list==null)?null:list.get(0);
			}else {
				logger.info("根据interface_type与platform_code查询对应信息失败");
				return null;
			}
		
		
	}
	/**
	 * 
	 * @param province_name 省份缩写标识
	 * @param platform_code 平台编码
	 * @return
	 */
	public CodeRelationship getCodeRelationship(String province_name , String platform_code){
		  if(StringUtils.isNotBlank(province_name)){
			  CodeRelationship codeRelationship = null;
			  try {			
				  logger.info("++++ redis get key: "+province_name+platform_code);
				   codeRelationship = (CodeRelationship)redisTemplate.opsForValue().get(province_name+platform_code);
			  	} catch (Exception e) {
					 logger.error("查询缓存异常"+e);
					 logger.info("查询缓存异常,查询数据库获取信息");
				}
				if(codeRelationship==null){
					 Map<String, Object> columnMap = Maps.newHashMap();
					 columnMap.put("platform_province_code", province_name);
					 columnMap.put("platform_type", platform_code);
					 List<CodeRelationship> list = codeRelationshipMapper.selectByMap(columnMap);
					 addCodeRelationship(province_name,platform_code,list.get(0),"");
					 codeRelationship=(list==null)?null:list.get(0);
				}
				  logger.info("根据省份标识platform_province_code："+province_name+",平台编码："+platform_code+"，查到的codeRelationship为："+codeRelationship);
				  return codeRelationship;						 
		  }else{
			  logger.info("省份标识为空");
			  return null;
		  } 
	}
	
	public void deleteInfo(String key){
		if(StringUtils.isNotBlank(key)){
			redisTemplate.delete(key);
		}else{
			logger.info("删除缓存失败");
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public void addInfo(String interface_type,String province_code,String platform_code,RequestSetting requestSetting){
		if(StringUtils.isNotBlank(interface_type) && StringUtils.isNotBlank(province_code) && null != requestSetting){
			redisTemplate.opsForValue().set(interface_type+province_code+platform_code,requestSetting);
		}else{
			logger.info("新增缓存失败");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void addCodeRelationship(String proviceId , String platform_code ,CodeRelationship codeRelationship,String oldKey){
		if (StringUtils.isNotBlank(proviceId)) {
			if (redisTemplate.hasKey(oldKey))
				redisTemplate.delete(oldKey);
			redisTemplate.opsForValue().set(proviceId + platform_code, codeRelationship);
		}else{
			logger.info("新增缓存失败");
		}
	}
	
}
